<template>
	<view class="content">
		<image class="logo" src="../../static/images/logo.png" mode="widthFix"></image>
		<view class="login">
			<view class="login2">
				<image src="../../static/phone.png" mode="aspectFit"/>
				<view class="login3">
					<input name="mobile" @input="shoujihao" type="text" placeholder="请输入您的常用手机号"/>
				</view>
			</view>
			<view class="login2">
				<view class="login30">
					<input name="mobile" @input="yan" type="text" placeholder="短信验证码"/>
				</view>
				<view class="zhuce" @click="send">{{yzm}}</view>
			</view>
			<view class="login2">
				<image src="../../static/pwd.png" mode="aspectFit"/>
				<view class="login3">
					<input name="password" @input="mima" type="password" password="true" placeholder="请输入登录密码"/>
				</view>
			</view>
			<view class="login2">
				<image src="../../static/pwd.png" mode="aspectFit"/>
				<view class="login3">
					<input name="password" @input="mima2" type="password" password="true" placeholder="请输入确认密码"/>
				</view>
			</view>
			<!--  #ifdef  MP-WEIXIN -->
			<view class="login4" @click="shouquan">注 册</view>
			<!--  #endif -->
			<!--  #ifndef  MP-WEIXIN -->
			<view class="login4" @click="zhuce">注 册</view>
			<!--  #endif -->
			
		</view>
		<view class="login6">
			<checkbox-group>
				<label>
					<checkbox class="xuanzen" value="cb" checked="true" color="#05c160" />注册登录即表示同意
				</label>
			</checkbox-group>
			<text @click="xieyi">《用户协议》</text><text style="color:#999;">和</text><text @click="yinsi">《隐私政策》</text>
		</view>
	</view>
</template>

<script>
	var app=getApp();
	var dingshi;
	export default {
		data() {
			return {
				mobile:'',
				password:'',
				password2:'',
				nickname:'',
				avatar:'',
				sex:0,
				code:'',
				yzm:'获取验证码',
				time:60,
				pid:0,
				is_send:false
			}
		},
		onLoad(op) {
			if(app.isBlank(op.uid)==false){
				this.pid=op.uid;
			}
		},
		methods: {
			shouquan(){
				var that=this;
				if(app.isBlank(that.avatar)==false){
					that.zhuce();
					return false;
				}
				uni.getUserProfile({
				  desc: '用于完善会员资料',
				  success: (res) => {
					that.nickname=res.userInfo.nickName
					that.avatar=res.userInfo.avatarUrl
					that.sex=res.userInfo.gender
					that.zhuce();
				  },
				  fail(r){
					  uni.showToast({
					      title: '授权失败，请重试',
						  icon:'none',
					      duration: 1000
					  });
				  }
				})
				
				
			},
			zhuce(){
				var that=this;
				if(app.isBlank(this.mobile)==true){
					uni.showToast({
					    title: '请输入手机号',
						icon:'none',
					    duration: 1500
					});
					return false;
				}
				if(app.isBlank(this.code)==true){
					uni.showToast({
					    title: '请输入短信验证码',
						icon:'none',
					    duration: 1500
					});
					return false;
				}
				if(app.isBlank(this.password)==true){
					uni.showToast({
					    title: '请输入登录密码',
						icon:'none',
					    duration: 1500
					});
					return false;
				}
				if(app.isBlank(this.password2)==true){
					uni.showToast({
					    title: '请输入确认密码',
						icon:'none',
					    duration: 1500
					});
					return false;
				}
				if(this.password2!=this.password){
					uni.showToast({
					    title: '两次输入密码不一致',
						icon:'none',
					    duration: 1500
					});
					return false;
				}
				uni.showLoading({
				    title: '请求中'
				});
				uni.request({
				    url: app.globalData.url+"index/zhuce",
					method:"POST",
					dataType:"json",
					header: {
					    'content-type': 'application/x-www-form-urlencoded'
					},
				    data: {mobile:that.mobile,pid:that.pid,password:that.password,password2:that.password2,code:that.code,nickname:that.nickname,avatar:that.avatar,sex:that.sex},
				    success: (res) => {
						uni.hideLoading();
						if(res.data.code!=1){
							uni.showToast({
							    title: res.data.msg,
								icon:'none',
							    duration: 1500
							});
							return false;
						}
						uni.showToast({
						    title: res.data.msg,
							icon:'success',
						    duration: 1500
						});
						uni.setStorageSync('uid', res.data.data.uid);
						if(that.pid>0){
							var pages = getCurrentPages();//当前页
							var beforePage = pages[pages.length - 2];//上个页面
							if(beforePage){
								// #ifdef H5
								beforePage.uid=res.data.data.uid
								// #endif
								// #ifndef H5
								beforePage.$vm.uid=res.data.data.uid
								// #endif
							}
						}
						setTimeout(function(){
							that.home();
						},1500)
				    }
				});
			},
			send(){
				if(this.is_send==true){
					return false;
				}
				var that=this;
				if(app.isBlank(this.mobile)==true){
					uni.showToast({
					    title: '请输入手机号',
						icon:'none',
					    duration: 1500
					});
					return false;
				}
				uni.showLoading({
				    title: '发送中'
				});
				uni.request({
				    url: app.globalData.url+"index/sms",
					method:"POST",
					dataType:"json",
					header: {
					    'content-type': 'application/x-www-form-urlencoded'
					},
				    data: {mobile:that.mobile,type:1},
				    success: (res) => {
						uni.hideLoading();
						if(res.data.code!=1){
							uni.showToast({
							    title: res.data.msg,
								icon:'none',
							    duration: 1500
							});
							return false;
						}
						uni.showToast({
						    title: res.data.msg,
							icon:'success',
						    duration: 1000
						});
						this.is_send=true;
						dingshi=setInterval(function(){
							that.shijian();
						},1000);
				    }
				});
			},
			shijian(){
				this.yzm=this.time+"S";
				this.time=this.time-1;
				if(this.time<0){
					this.yzm="重新获取";
					this.time=60;
					this.is_send=false;
					clearInterval(dingshi);
				}
			},
			tui(){
				uni.navigateBack({
				    delta: 1
				});
			},
			xieyi(){
				uni.navigateTo({
				    url: '/pages/user/danye?id=1'
				});
			},
			yinsi(){
				uni.navigateTo({
				    url: '/pages/user/danye?id=2'
				});
			},
			home(){
				var pages = getCurrentPages(); //当前页面栈
				if (pages.length > 1){
					uni.navigateBack({
						delta: 2
					});
				}else{
					uni.reLaunch({
					    url: '/pages/index/index'
					});
				}
			},
			shoujihao: function(event) {
			    this.mobile = event.target.value
			},
			mima: function(event) {
			    this.password = event.target.value
			},
			mima2: function(event) {
			    this.password2 = event.target.value
			},
			yan: function(event) {
			    this.code = event.target.value
			}
		}
	}
</script>

<style>
	.login6{color:#46C706;float: left;line-height: 25px;margin-top: 15px;font-size: 0.75rem;margin-bottom: 10px;}
	.login6 text,checkbox-group{float: left;}
	.zhuce{width:120px;height:40px;line-height: 40px;text-align: center;background-color: #46C706;color:#fff;margin-left: 10px;}
	.login30 input{width:100%;height:40px;text-align: center;}
	.login30{width:100%;height:40px;flex:1;}
	.xuanzen{transform:scale(0.7);}
	label{line-height: 25px;color: #999;}
	.login5{width:100%;height:30px;line-height: 30px;overflow: hidden;margin-top: 15px;text-align: center;}
	.login4{width:100%;height:40px;line-height: 40px;text-align: center;background-color: #46C706;color:#fff;border-radius: 3px;margin-top: 30px;}
	.login3 input{width:100%;height:40px;}
	.login3{width:100%;height:40px;flex:1;}
	.login2 image{width:22px;height:22px;margin-right: 8px;margin-top: 9px;margin-left: 5px;}
	.login2{width:100%;height:40px;background-color: #ececec;border-radius: 3px;display: flex;margin-bottom: 20px;}
	.login{width:80%;}
	.logo{width:180px;margin: 10px auto;}
</style>
